package main.entities;


import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.state.StateBasedGame;
import org.newdawn.slick.tiled.TiledMap;

import main.states.Play;
import main.tileMap.Map;


public class EntityPlayer extends Entity {

	public double moveSpeed = 1.6;
	
	public static int rX, rY;
	
	public static boolean isMoving = false;
	
	public int aniFrame = 0;
	public int aniTime = 12;
	public int aniDelta = 0;

	private boolean up= false,down=false,right=false,left=false;


	private boolean moving = false;

	private int dy;

	private int dx;

	private boolean finish = true;

		
	
	public EntityPlayer(String s,double x, double y, double width, double height,Map map){
		super(s,x, y, width, height);
		
		moveSpeed = 0;
		maxSpeed = 2;
		aceleration =0.2;
		this.map = map;
		this.health = 110;
	}
	
	
	public void move(){
			if(dx==0&&dy==0) moving =false;
			if(moving&&finish) moveSpeed += aceleration;
			if(moveSpeed>maxSpeed){
			moveSpeed = maxSpeed;
			finish = false;
			}
			if(!moving&&!finish) moveSpeed -=aceleration;	
			if(moveSpeed<0){
			moveSpeed = 0;
			finish = true;	
		
			}
		
			 map.playerY += moveSpeed/32*dy;

			 map.playerX += moveSpeed/32*dx;

		
		
		
	}
	@Override
	public void render(Graphics g){
	g.drawImage(image,(float)x, (float)y,null);
	}
	@Override
	public void collideWith(Entity entity) {
		// TODO Auto-generated method stub
		
	}
	
	public void update(GameContainer gc, StateBasedGame sbg, int g){
			finish = false;
			if(up&&!down&&tryToMove((float)map.playerX, (float) (map.playerY-(maxSpeed-32)/32))){
				finish =true;
				dy=-1;

			}else
			if(!up&&down&&tryToMove((float)map.playerX, (float) (map.playerY+(moveSpeed)/32))){
				 finish =true;
				dy=1;
	
			}else{
				if(finish){
					dy = 0;
				}

			}
			if(left&&!right&&tryToMove((float)map.playerX, (float) (map.playerY-moveSpeed/32))){
				 finish =true;
				dx=-1;

				
			}else
			if(right&&!left&&this.tryToMove((float)map.playerX, (float) (map.playerY-moveSpeed/32))){
				 finish =true;
				dx=1;

				
			}else{
				if(finish){
					dx=0;
				}
			}
			
			move();
		
	}
	public void keyPressed(int k, char c) {
		if(k == Input.KEY_UP) up = true; moving = true;
		if(k == Input.KEY_DOWN) down = true; moving = true;
		if(k == Input.KEY_LEFT) left = true; moving = true;
		if(k == Input.KEY_RIGHT) right = true; moving = true;
		
	}

	public void keyReleased(int k, char c) {
		if(k == Input.KEY_UP)   up = false; moving = false; finish =false;
		if(k == Input.KEY_DOWN) down = false;moving = false;finish =false;
		if(k == Input.KEY_LEFT) left = false; moving = false;finish =false;
		if(k == Input.KEY_RIGHT)right = false;moving = false;finish =false;
		
	}


	
	
}
